package com.ldc.commonlibs.http

import com.ldc.commonlibs.uts.DataStoreUts
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response


/**
 *@description
 *@author liudc
 *@data 2022/6/15 23:31
 *
 */
class WanAndroidCookieJar : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        return chain.proceed(buildResponse(request)!!)

    }

    private fun buildResponse(request: Request): Request? {
        if (!request.url.encodedPath.contains("/user/login", true)) {
            val cookies: List<String> = DataStoreUts.INSTANCE.getCookie().split(";")
            val build: Request.Builder = request.newBuilder()
            cookies.forEach {
                build.addHeader("Cookie", it)
            }
            return build.build()
        }
        return request
    }

}